package cn.elwy.rcp.ui.table;

import java.util.HashMap;
import java.util.List;

import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import cn.elwy.rcp.ui.table.internal.TableColumnSortListener;
import cn.elwy.rcp.ui.table.listener.TablePaneCheckStateListener;
import cn.elwy.rcp.ui.table.model.ColumnInfo;
import cn.elwy.rcp.ui.table.model.TableInfo;
import cn.elwy.rcp.ui.table.provider.TablePaneCheckStateProvider;
import cn.elwy.rcp.ui.table.setting.listener.ColumnResizeListener;
import cn.elwy.rcp.ui.table.util.ColumnVisibleUtil;
import cn.elwy.rcp.ui.util.SWTUtil;
import cn.elwy.rcp.ui.util.WidgetUtil;

public class TableViewerPane extends TablePane {

  protected TableViewer tableViewer;
  Table table;

  protected TableViewerPane(Composite parent, String tableId) {
    super(parent, SWT.NONE, tableId);
  }

  protected TableViewerPane(Composite parent, int sytle, String tableId) {
    super(parent, sytle, tableId);
  }

  protected TableViewerPane(Composite parent, int sytle, TableInfo tableInfo) {
    super(parent, sytle, tableInfo);
  }

  @Override
  protected void createTableViewer() {
    TablePaneCheckStateProvider checkStateProvider = tableInfo.getCheckStateProvider();
    if (checkStateProvider == null) {
      tableViewer = new TableViewer(this, getStyle());
    } else {
      Table table = new Table(this, SWT.CHECK | getStyle());
      tableViewer = new CheckboxTableViewer(table);
      ((CheckboxTableViewer) tableViewer).setCheckStateProvider(checkStateProvider);
    }

    table = tableViewer.getTable();
    table.setLayoutData(new GridData(GridData.FILL_BOTH));
  }

  @Override
  protected void createColumns() {
    treeColumnMap = new HashMap<String, Item>();

    boolean canSettingColumn = tableInfo.isCanSettingColumn();
    List<ColumnInfo> columnInfos = tableInfo.getColumnInfoList();
    for (ColumnInfo columnInfo : columnInfos) {
      int style = SWTUtil.toSWTConstant(columnInfo.getStyleName());
      TableViewerColumn treeColumn = new TableViewerColumn(tableViewer, style);
      TableColumn column = treeColumn.getColumn();
      column.setText(columnInfo.getName());
      column.setWidth(columnInfo.getWidth());
      int alignment = SWTUtil.toSWTConstant(columnInfo.getAlignmentName());
      column.setAlignment(alignment);
      if (columnInfo.getWidth() == -1) {
        WidgetUtil.autoColumnWidth(table);
      }
      column.setData(ColumnVisibleUtil.DEFAULT_COLUMN_WIDTH, columnInfo.getWidth());
      column.setData(columnInfo.getId());
      column.setData("columnInfo", columnInfo);
      column.setMoveable(true);
      column.setResizable(true);
      ColumnVisibleUtil.setColumnVisible(column, columnInfo.isVisible());
      if (canSettingColumn) {
        ColumnResizeListener listener = new ColumnResizeListener();
        listener.setTablePane(this);
        column.addControlListener(listener);
      }
      treeColumnMap.put(columnInfo.getId(), column);
    }
  }

  @Override
  protected void addColumnSortListener() {
    TableColumn[] columns = table.getColumns();
    for (TableColumn column : columns) {
      column.addSelectionListener(new TableColumnSortListener(tableViewer));
    }
  }

  @Override
  protected void addCheckStateListener() {
    TablePaneCheckStateListener checkStateListener = tableInfo.getCheckStateListener();
    if (checkStateListener != null && tableViewer instanceof CheckboxTableViewer) {
      checkStateListener.setTablePane(this);
      ((CheckboxTableViewer) tableViewer).addCheckStateListener(checkStateListener);
    }
  }

  @Override
  protected void addDropListener() {
  }

  @Override
  protected void addDragListener() {
  }

  @Override
  public ColumnViewer getTableViewer() {
    return tableViewer;
  }

  @Override
  public Table getTable() {
    return table;
  }

  @Override
  public int[] getColumnOrder() {
    return table.getColumnOrder();
  }

  @Override
  public void setColumnOrder(int[] columnOrder) {
    table.setColumnOrder(columnOrder);
  }

  @Override
  public Item[] getColumns() {
    return table.getColumns();
  }
}